package com.langsin.shihao;

import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.shihao.mapper.GoodsMapper;
import com.langsin.shihao.mapper.TeacherMapper;
import com.langsin.shihao.mapper.UserMapper;
import com.langsin.shihao.mapper.WorkerMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class MyTest {
    SqlSession sqlSession=null;
    //每次单元测试类之前执行
    @Before
    public void before() throws Exception{
        //读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //获取一个会话（连接）默认不提交
         sqlSession = sqlSessionFactory.openSession();
    }
    //每次单元测试类之后执行
    @After
    public void after() throws Exception{
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void testMapper1() throws Exception{
        //获取mapper接口（dao）的动态代理实现类
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        List<Worker> workers = workerMapper.selectByID(50);
        workers.forEach(System.out::println);

    }
    @Test
    public void testMapper2() throws Exception{
   Worker worker=new Worker();
   //主键 可以整数自增 ，字符串uuid，雪花算法
   worker.setWid(IdUtil.getSnowflakeNextIdStr());
   worker.setWname("大龙女");
   worker.setSex("女");
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.insertWorker(worker);
        System.out.println(row);

    }
    @Test
    public void testMapper3() throws Exception{
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        Worker worker=new Worker();
        worker.setWname("警察");
        worker.setAge(30);
        worker.setSex("女");
        int i = workerMapper.insertWorker2(worker);
        System.out.println(i+"------"+worker);


    }
    @Test
    public void testMapperGoods() throws Exception{
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        Goods goods=new Goods();
        goods.setGoodsName("Green typeC数据线");
        goods.setShopPrice(19.88);
        int i = goodsMapper.addGoods(goods);
        System.out.println(goods);

    }
    @Test
    public void testMapperlike() throws Exception{
      String name="%C%";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName(name);
        for (Goods good : goods) {
            System.out.println(good);
        }

    }
    @Test
    public void testMapperlike2() throws Exception{
        String name="C";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName2(name);
        for (Goods good : goods) {
            System.out.println(good);
        }

    }
    @Test
    public void testMapperUser() throws Exception{
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        Integer uid=702;
        User user = mapper.queryByID(uid);
        System.out.println(user);
    }
    @Test
    public void testtea() throws Exception{
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Integer tid=1009;
        Teacher teacher = mapper.queryByID(tid);
        System.out.println(teacher);
    }
    @Test
    public void testmap() throws Exception{
        QueryVo queryVo=new QueryVo();
        queryVo.setRoleCode("manager");
        queryVo.setAddress("济南浪曦");
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.queryByRoleAndAddress(queryVo);
        for (User user : users) {
            System.out.println(user);
        }

    }

    @Test
    public void test1() throws Exception{
        //读取配置文件
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        //工厂
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        //获取一个会话（连接）默认不提交
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //查询一个worker ”68c8d109-5e5d-11e9-9dc0-28d244d68b7c“
        Worker worker = sqlSession.selectOne("worker.selectUser", "6c6f2776-5e5d-11e9-9dc0-28d244d68b7c");
        System.out.println(worker);
    }
    //You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
    @Test
    public void testupdate() throws Exception {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(stream);
        SqlSession sqlSession = build.openSession();
        Worker worker=new Worker();
        worker.setAge(500);
        worker.setWid("5af9f088-5e5d-11e9-9dc0-28d244d68b7c");
        int row = sqlSession.update("worker.updateAgeByID", worker);
        System.out.println(row);
//java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for user.updateAgeByID
    }

}
